package com.zpf.web.servlet;

import com.alibaba.druid.util.StringUtils;
import com.zpf.pojo.Goods;
import com.zpf.pojo.PageBean;
import com.zpf.service.GoodsService;
import com.zpf.service.impl.GoodServiceImpl;
import com.zpf.utils.StrUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/goodsservlet")
public class GoodsServlet extends BaseServlet {
    GoodsService goodsService = new GoodServiceImpl();

    public String getGoodsById(HttpServletRequest request, HttpServletResponse response) throws Exception {
        int id =Integer.parseInt(request.getParameter("id")) ;
        Goods goods = goodsService.findById(id);

        request.setAttribute("goods",goods);
        return "forward:/goodsDetail.jsp";

    }

    public String getGoodsListByTypeId(HttpServletRequest request, HttpServletResponse response) throws Exception {
    //获取分类id
      int typeId =  Integer.parseInt(request.getParameter("typeId"));
        String pageNumStr = request.getParameter("pageNum");
        String pageSizeStr = request.getParameter("pageSize");
        int pageNum = 1;
        int pageSize = 4;
        if(StrUtils.isNotEmpty(pageNumStr)){
            pageNum = Integer.parseInt(pageNumStr);
        }

        if(StrUtils.isNotEmpty(pageSizeStr)){
            pageSize = Integer.parseInt(pageSizeStr);
        }
       PageBean pageBean= goodsService.findbypage(typeId,pageNum,pageSize);
        request.setAttribute("pageBean",pageBean);
        request.setAttribute("typeId", typeId);
        return"forward:/goodsList.jsp";
    }
    }
